package com.fengxu.netty.customercodec;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;


public class MyClient {

    public static void main(String[] args) throws InterruptedException {

        NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);

        Bootstrap bootstrap = new Bootstrap();

        bootstrap.group(bossGroup)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new MyByteToLongEncoder());
                        ch.pipeline().addLast(new SimpleChannelInboundHandler<Long>() {
                            @Override
                            protected void channelRead0(ChannelHandlerContext ctx, Long msg) throws Exception {

                            }

                            @Override
                            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                System.out.println("客户端发送数据!");
                                ctx.writeAndFlush(1234L);
                            }
                        });
                    }
                });
        bootstrap.connect("127.0.0.1",6666).sync()
                .channel().closeFuture().sync();
    }
}
